package team.se.tms.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;
import team.se.tms.util.EncryptUtil;

/**
 * 自定义密码比较器
 *
 * @author winkyle
 * @date 2018/11/17
 */
@Slf4j
public class CredentialsMatcher extends SimpleCredentialsMatcher {
    /**
     * 重写密码比较方法
     *
     * @param authenticationToken token
     * @param authenticationInfo info
     * @return 将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
     * @create on 2018/11/17 by winkyle
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        Object tokenCredentials = EncryptUtil.sha1(String.valueOf(token.getPassword()), (String)authenticationInfo.getPrincipals().getPrimaryPrincipal());
        Object accountCredentials = getCredentials(authenticationInfo);
        return equals(tokenCredentials, accountCredentials);
    }


}
